package com.learn.dict.handler;

import cn.hutool.cache.Cache;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.learn.dict.domain.vo.DictDataOptions;
import com.learn.dict.domain.vo.DictTypeOptions;
import com.learn.dict.handler.business.IBusinessDictHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 功能描述: 字典加载抽象类 <br/>
 *
 * @author: MI
 * @email: 448341911@qq.com
 * @createDate: 2023/10/22 22:43
 * @updateUser: MI
 * @updateDate: 2023/10/22 22:43
 * @updateRemark: 修改内容
 * @version: v1.0
 */
@Component
public class BusinessDictHandler extends AbstractDictHandler {


    @Resource
    private DictHandlerAware dictHandlerAware;

    public BusinessDictHandler() {
    }

    @Override
    public List<DictDataOptions> getDictDataOptions(String typeCode) {
        return null;
    }


    @Override
    public void loadDictCache() {
        // 查找IBusinessDictHandler的所有实现
        Set<Map.Entry<String, IBusinessDictHandler>> entries = dictHandlerAware.getHandlerMap().entrySet();
        // 循环添加
        for (Map.Entry<String, IBusinessDictHandler> entry : entries) {
            addDictType(entry.getKey(), entry.getValue().typeName());
            put(entry.getKey(), entry.getValue().getDictDataList());
        }
    }

    @Override
    void reloadDictCache(String typeCode) {
        remove(typeCode);
        IBusinessDictHandler handler = dictHandlerAware.getHandlerMap().get(typeCode);
        if (ObjectUtil.isNotNull(handler)) {
            List<DictDataOptions> dictDataList = handler.getDictDataList();
            if (CollUtil.isNotEmpty(dictDataList)) {
                put(typeCode, dictDataList);
            }
        }
    }
}
